﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.Utilities;
namespace Web.Cms.Content.Zones
{
    public partial class CreateEditZone : AdminPage
    {
        #region Khai báo
        private int zoneId = 0;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                zoneId = ObjConvertUtility.ToInt32(Request.QueryString["ZoneIDLang"]);
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                ModulType.AddDropList(DropModuleMenu);
                CMSZonesBL.GetSub(dropParent, "0", Const.VN, 0, 0);
                ListPriority.ByDropList(dropPriority, 50);
                if (zoneId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    CMSZonesInfo info = CMSZonesBL.Select(zoneId);
                    if (info != null)
                    {
                        info.ZoneID = 0;
                        info.ZoneIDLang = 0;
                        dropParent.SelectedValue = info.ParentID.ToString();
                        txtZonesName.Text = info.Title;
                        dropPriority.SelectedValue = info.Priority.ToString();
                        info.Lang = 1;
                        DropModuleMenu.SelectedValue = info.ModuleMenu.ToString();
                        txtImage.Text = info.ImagesZone;
                        DropClick.SelectedValue = info.StatusClick;
                        ListBoxTrangHienThi(dropTrangHienThi, info.ModuleMenu.ToString());
                        dropTrangHienThi.SelectedValue = info.TrangHienThi.ToString();
                        txtLinkUrl.Text = info.ZoneUrl;
                        SelectedCheckBox(info);
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                CMSZonesInfo info = new CMSZonesInfo();
                FillData(info);
                int id = CMSZonesBL.Insert(info);
                CMSZonesBL.UpdateZoneUrl(id, CMSZonesBL.GetZoneUrl(id, 1, txtLinkUrl.Text));              
                if (id > 0)
                {                  
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region chỉnh sửa
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (zoneId == Convert.ToInt32(dropParent.SelectedValue))
                {
                    Response.Write("<script language=\"javascript\">alert(\"Bạn chọn sai chuyên mục ! \")</script>");
                }
                else
                {
                    CMSZonesInfo info = new CMSZonesInfo();
                    FillData(info);
                    CMSZonesBL.Update(info);                   
                    CMSZonesBL.UpdateZoneUrl(zoneId, CMSZonesBL.GetZoneUrl(zoneId, 1, txtLinkUrl.Text));                    
                    lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CMSZonesInfo info)
        {
            if (zoneId > 0)
                info.ZoneID = zoneId;
            info.ZoneIDLang = 0;
            info.ParentID = ObjConvertUtility.ToInt32(dropParent.SelectedValue);
            info.Title = txtZonesName.Text.Trim();
            info.Priority = ObjConvertUtility.ToInt32(dropPriority.SelectedValue);
            info.Lang = 1;
            info.CreatedOn = DateTime.Now;
            info.CreatedBy = CurrentUser.UserID;
            info.DeletedOn = new DateTime(1980, 1, 1);
            info.DeletedBy = 0;
            info.KeyZone = ObjMiscUtility.Replace(txtZonesName.Text.Trim()); ;
            info.ModuleMenu = ObjConvertUtility.ToInt32(DropModuleMenu.SelectedValue);
            info.ImagesZone = txtImage.Text.Trim();
            info.StatusClick = DropClick.SelectedValue.Trim();
            info.CheckZone = CheckZone();
            info.TrangHienThi = Request.Form[dropTrangHienThi.UniqueID];

        }
        #endregion

        #region CheckZone
        int CheckZone()
        {
            CheckBox[] Boxes = new CheckBox[6];

            Boxes[0] = chMenuTop;
            Boxes[1] = chMenuLeft;
            Boxes[2] = chMenuRight;
            Boxes[3] = chBottom;
            Boxes[4] = chMenuRss;
            Boxes[5] = chMenuWap;
            return ClsCheckBox.ReturnStatusCheck(Boxes);
        }
        #endregion

        #region SelectedCheckBox
        void SelectedCheckBox(CMSZonesInfo zoneInfo)
        {
            CheckBox[] Boxes = new CheckBox[6];

            Boxes[0] = chMenuTop;
            Boxes[1] = chMenuLeft;
            Boxes[2] = chMenuRight;
            Boxes[3] = chBottom;
            Boxes[4] = chMenuRss;
            Boxes[5] = chMenuWap;

            chMenuTop.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.menuTop, zoneInfo.CheckZone, Boxes);
            chMenuLeft.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.menuLeft, zoneInfo.CheckZone, Boxes);
            chMenuRight.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.menuRight, zoneInfo.CheckZone, Boxes);
            chBottom.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.menuBottom, zoneInfo.CheckZone, Boxes);
            chMenuRss.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.menuRss, zoneInfo.CheckZone, Boxes);
            chMenuWap.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.menuWap, zoneInfo.CheckZone, Boxes);
        }
        #endregion

        #region Load ListBox trang hien thi
        void ListBoxTrangHienThi(DropDownList dll, string values)
        {
            dll.Items.Clear();
            if (values == "1")
            {
                dll.Items.Add(new ListItem("Trang chủ", "12"));
            }
            else if (values == "2")
            {
                dll.Items.Add(new ListItem("Trang tin tức", "21"));
            }
            else if (values == "3")// Sản phẩm
            {
                dll.Items.Add(new ListItem("Hiển thị dạng danh sách", "31"));
            }
            else if (values == "4")// Sản phẩm
            {
                dll.Items.Add(new ListItem("---------------------", "41"));
            }
            else if (values == "5")// Sản phẩm
            {
                dll.Items.Add(new ListItem("---------------------", "51"));
            }  
            else
                dll.Items.Add(new ListItem("Trang chủ", "12"));
        }
        #endregion
    }
}